Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

coop: expose coop as a public module #7116

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

maminrayej
Copy link
Member

This PR makes the coop module public and exposes the has_budget_remaining function.

The motivation for this is to make select! budget-aware (see #7108). The exposed functionality is also useful when dealing with a combination of budget-aware and non-budget-aware futures.

Related to #7111.

@maminrayej maminrayej added A-tokio Area: The main tokio crate M-coop Module: tokio/coop labels Jan 21, 2025
@github-actions github-actions bot added R-loom-current-thread Run loom current-thread tests on this PR R-loom-multi-thread Run loom multi-thread tests on this PR R-loom-multi-thread-alt Run loom multi-thread alt tests on this PR R-loom-sync Run loom sync tests on this PR labels Jan 21, 2025
@github-actions github-actions bot removed R-loom-sync Run loom sync tests on this PR R-loom-multi-thread-alt Run loom multi-thread alt tests on this PR R-loom-multi-thread Run loom multi-thread tests on this PR R-loom-current-thread Run loom current-thread tests on this PR labels Jan 21, 2025
@maminrayej maminrayej added the M-runtime Module: tokio/runtime label Jan 21, 2025
tokio/tests/coop_budget.rs Outdated Show resolved Hide resolved
tokio/src/runtime/coop.rs Outdated Show resolved Hide resolved
tokio/src/runtime/coop.rs Outdated Show resolved Hide resolved
@github-actions github-actions bot added R-loom-current-thread Run loom current-thread tests on this PR R-loom-multi-thread Run loom multi-thread tests on this PR R-loom-multi-thread-alt Run loom multi-thread alt tests on this PR R-loom-sync Run loom sync tests on this PR labels Jan 22, 2025
tokio/src/io/util/copy.rs Show resolved Hide resolved
tokio/src/task/mod.rs Outdated Show resolved Hide resolved
tokio/src/task/coop/mod.rs Outdated Show resolved Hide resolved
tokio/src/task/coop/mod.rs Outdated Show resolved Hide resolved
tokio/src/task/mod.rs Outdated Show resolved Hide resolved
tokio/tests/coop_budget.rs Outdated Show resolved Hide resolved
Comment on lines +296 to +301
#[doc(hidden)]
#[deprecated = "Moved to tokio::task::coop::unconstrained"]
pub use coop::unconstrained;
#[doc(hidden)]
#[deprecated = "Moved to tokio::task::coop::Unconstrained"]
pub use coop::Unconstrained;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move these next to consume_budget so they are grouped together?

Comment on lines +172 to +181
/// Futures created by the tokio library functions are budget-aware and yield when there is no more
/// budget left, but not all futures will be budget-aware. Consider future `A` that polls two inner
/// futures `B` and `C`, and returns `Poll::Ready` when one of them is ready. If both inner futures
/// were budget-aware, at some point the budget would be depleted which would cause both futures to
/// return `Poll::Pending` resulting in `A` returning `Poll::Pending` as well. Yielding all the way
/// back to the runtime, the budget would be reset, and `B` and `C` could make progress again.
/// Now let's consider `B` is budget-aware, but `C` is not and is always ready. The budget will be
/// depleted as before, but now since `C` always returns `Poll::Ready`, `A` will always return
/// `Poll::Ready` as well. This way, `A` will not yield back to the runtime which will keep the budget
/// depleted and `B` not making progress.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This explanation is confusing. And more generally, this seems like a niche use-case. Are there not more common use-cases for this that we can explain instead?

E.g. maybe just have this example:

if !has_budget_remaining() {
    tokio::task::yield_now().await;
}

and say that this is similar to consume_budget, but doesn't decrement the budget when non-zero.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate M-coop Module: tokio/coop M-runtime Module: tokio/runtime R-loom-current-thread Run loom current-thread tests on this PR R-loom-multi-thread Run loom multi-thread tests on this PR R-loom-multi-thread-alt Run loom multi-thread alt tests on this PR R-loom-sync Run loom sync tests on this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants